### IMPORTS
import pickle
import os
from pprint import pprint

import domination
from utilities import *

### FUNCTIONS ###

def run(settings, n_games=200, handicaps=1, start_at=0.00, fn='hcg.csv'):
    """ Run n games and evaluate average score and 
        score standard deviation.
    """
    for i in xrange(handicaps):
        handicap = i*0.05+start_at
        scores = []
        for i in xrange(n_games):
            g = domination.Game(red_brain='agent_adjustable.py',blue_brain='agent_adjustable.py',
                                red_init={'handicap':handicap},
                                rendered=False, settings=settings, record=True)
            g.run()
            if g.agent_raised_exception:
                pickle.dump(g.replay,open('replay-exception','w'))
            score_rel = g.score_red/float(settings.max_score)
            scores.append(score_rel)
            print "Report:\nAverage score: %.3f\nStandard dev.: %.3f"%(mean(scores),stdev(scores))
        pf = '%.2f,'%handicap
        f = open(os.path.join('results',fn),'a')
        f.write(pf + ','.join("%.3f"%s for s in scores))
        f.write('\n')
        f.close()

### PROCEDURE
settings = domination.Settings(max_steps=400,
 			       ammo_rate=20,
			       spawn_time=20,
			       ammo_amount=3,
		 	       cp_handle=2,
                               think_time=0.025)
run(settings, handicaps=1, start_at=0.45, n_games=200, fn='hcg_cp_majority.csv')
    
